'use strict';
/**
 * 登录管理Service
 */
app.service('LoginService',
		['$http', '$window', '$q', 'dialogs', '$translate', '$state', '$timeout', '$localStorage', '$rootScope', 'IPermissionService',
function($http, $window, $q, dialogs, $translate, $state, $timeout, $localStorage, $rootScope, IPermissionService){
	var that = this;
	this.login = function(user){
		return $http.post(ISS.getPath("login"), user).then(function(resp){
					return resp.data;
				});
	}
	//退出
	this.logout = function(){
		$http.post(ISS.getPath("logout/"),{token:$localStorage.token}).then(function(resp){
			if(resp.data.success){
				that.toLogin();
			}
		});
	}
	var errorDialog = null;
	this.notify = function(data, statusCode, config){
		if(errorDialog == null){
			if(statusCode == 401 && !$state.includes('login')){//session timeout
				//never login
				if(null == that.logined){
					$state.go('login');
				}else{
					errorDialog = dialogs.notify($translate.instant("dialogs.SESSION_TIMEOUT"));
					//关闭时重置url
					errorDialog.closed.then(function(){
						errorDialog = null;
						$rootScope.$broadcast("$stateChangeError");
						$state.go('login');
					});
				}
			}else if(statusCode == 499){//need License
				$rootScope.$broadcast("$stateChangeError");
				$state.go('sysManagement.licenseMain');
			}else if(statusCode == -1){//Server Connection Refused
				dialogs.notify(getMessage("common.serverNotRespose"));
			}else if(data && data.success === false){
				dialogs.notify(data);
			}
		}
	}
	//校验Licence
	this.validLicense = function(){
		$.ajax({
			url: ISS.getPath('sm/license/validLicense'),
			dataType: 'json',
			async: false,
			success: function(data){
				if(data.success !== true){
					$timeout(function(){$state.go('app.sm.license')});
				}
			}
		});
	}
	this.logined = null;
	this.getLoginUser = function(config){
		var deferred = $q.defer();
		if(ISS.isDebug){
			deferred.resolve(true);
			return deferred.promise;
		}else{
			return $http.post(ISS.getPath('user/getLoginUser'),null,config).then(function(resp){
				var data = resp.data;
				that.logined = {};
				that.logined.loginUser = data.loginUser;
	    		that.logined.permission = data.privilegeCode;
				that.logined.pwdRule = data.pwdRule;
				return that.logined;
			});
		}
	}
	//检查权限,页面使用
	this.checkPermission = function(code){
		if(this.logined != null && this.logined.permission != null){
			return this.logined.permission[code] == null ? false : true;
		}
		return false;
	}
	//校验权限,路由跳转使用
	this.hasPermission = function(code){
		var deferred = $q.defer();
		if(ISS.isDebug){
			deferred.resolve(true);
		}else{
			if(that.logined == null){
				that.getLoginUser().then(function(){
					if(that.logined.permission[code]){
						deferred.resolve(true);
					}else{
						deferred.reject(false);
					}
				});
			}else{
				if(that.logined.permission[code]){
					deferred.resolve(true);
				}else{
					deferred.reject(false);
				}
			}
		}
		return deferred.promise;
	}
	
	IPermissionService.hasPermission = this.hasPermission;
}]);